package com.example.zhaicaoapplicationv3.Cehua_menu.WeiDu;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.example.zhaicaoapplicationv3.Book.Book_Activity;
import com.example.zhaicaoapplicationv3.ShouYe_Main.MainActivity;
import com.example.zhaicaoapplicationv3.MyGridViewPackage.MyGridViewAdapter;
import com.example.zhaicaoapplicationv3.R;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 书架页面的主页面，实现利用GridView显示的摘抄集功能
 * @author lxx
 * 2019.10.19
 */

public class WeiDuFragment extends Fragment implements AdapterView.OnItemLongClickListener{

    private View root;
    private GridView mGridView;
    private MyGridViewAdapter mAdapter;
    private boolean isShowDelete = false;

    private ArrayList<HashMap<String, Object>> myList;
    /**
     * 测试用例
     * @author lxx
     * 2019.10.19
     */
    final String[] namee = new String[]{
            "书名1(摘抄集)", "书名2(摘抄集)","书名3(摘抄集)", "书名4(摘抄集)",
            "书名5(摘抄集)", "书名6(摘抄集)","书名7(摘抄集)", "书名8(摘抄集)"};
    final int[] picc = new int[]{
            R.drawable.ceshi,R.drawable.ceshi,
            R.drawable.ceshi,R.drawable.ceshi,
            R.drawable.ceshi,R.drawable.ceshi,
            R.drawable.ceshi,R.drawable.ceshi,
    };
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_weidu, container, false);
        /**
         * //数组初始化并绑定公共类MyGridViewAdapter，实现长按事件的监听并初始化数组
         * @author lxx
         * 2019.10.19
         */
        mGridView =  root.findViewById(R.id.gridView_2);
        mGridView.setOnItemLongClickListener(this);//长按事件监听
        myList = getMenuAdapter(picc,namee);

        mAdapter = new MyGridViewAdapter(getActivity(), myList);
        mGridView.setAdapter(mAdapter);
        ImageView imageView_book = root.findViewById(R.id.book_unchosed);
        imageView_book.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);
                //取消跳转动画
                overridePendingTransition(0, 0);
            }
        });
        //设置GridView中item为单击事件
        GridView gridView = root.findViewById(R.id.gridView_2);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), Book_Activity.class);
                startActivity(intent);
                //取消跳转动画功能
                overridePendingTransition(0, 0);
            }
        });
        return root;
    }

    private void overridePendingTransition(int i, int i1) {

    }

    //GridView部分代码
    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
        if (isShowDelete) {
            isShowDelete = false;
        } else {
            isShowDelete = true;
            mAdapter.setIsShowDelete(isShowDelete);

            mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                    delete(position);//删除选中项
                    mAdapter = new MyGridViewAdapter(getActivity(), myList);//重新绑定一次adapter
                    mGridView.setAdapter(mAdapter);
                    mAdapter.notifyDataSetChanged();//刷新gridview
                }

            });
        }

        mAdapter.setIsShowDelete(isShowDelete);//setIsShowDelete()方法用于传递isShowDelete值
        return true;
    }


    private void delete(int position) {//删除选中项方法
        ArrayList<HashMap<String, Object>> newList = new ArrayList<HashMap<String, Object>>();
        if (isShowDelete) {
            myList.remove(position);
            isShowDelete = false;
        }
        newList.addAll(myList);
        myList.clear();
        myList.addAll(newList);

        //重新将items设置为单击事件
        GridView gridView = root.findViewById(R.id.gridView_2);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), Book_Activity.class);
                startActivity(intent);
                //取消跳转动画功能
                overridePendingTransition(0, 0);
            }
        });
    }

    private ArrayList<HashMap<String, Object>> getMenuAdapter(//将数据装入List
                                                              int[] menuImageArray, String[] menuNameArray) {
        ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < menuImageArray.length; i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("image", menuImageArray[i]);
            map.put("name",menuNameArray[i]);
            data.add(map);
        }
        return data;
    }
}